﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineTestBLL;
using OnlineTestModel;
using FineUI;

namespace OnlineTestUI
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            InitCaptchaCode();
        }
        /// <summary>
        /// 初始化验证码
        /// </summary>
        private void InitCaptchaCode()
        {
            // 创建一个 6 位的随机数并保存在 Session 对象中
            Session["CaptchaImageText"] = GenerateRandomCode();
            imgCaptcha.ImageUrl = "~/captcha/captcha.ashx?w=150&h=30&t=" + DateTime.Now.Ticks;
        }

        /// <summary>
        /// 创建一个 6 位的随机数
        /// </summary>
        /// <returns></returns>
        private string GenerateRandomCode()
        {
            string s = String.Empty;
            Random random = new Random();
            for (int i = 0; i < 6; i++)
            {
                s += random.Next(10).ToString();
            }
            return s;
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InitCaptchaCode();
        }
        protected void BtAdd_Click(object sender, EventArgs e)
        {
            Students s = new Students();
            StudentsBLL sb = new StudentsBLL();
            s.UserName = tbxUserName.Text;
            s.PassWord = tbxPassword.Text;
            s.RealName = tbxRealName.Text;
            s.ClassId = 0;
            s.RoleId = 0;
            s.RoleList = "";
            s.Status = 0;
            s.CreateId = 0;
            s.CreateTime = DateTime.Now;
            s.LastLoginTime = DateTime.Now;
            int result = sb.Add(s);
            if (result > 0)
            {
                Session["student"] = s;
                PageContext.Redirect("default.aspx");
            }

        }
    }
}